package cn.tom.basic.util;

import cn.tom.basic.exception.StatusCode;
import lombok.Data;

/**
 * 工具类：封装除了查询之后的所有ajax请求的结果 - 返回值统一规范【方便维护】
 */
@Data
public class AjaxResult {

    private Boolean success = true;  //前端通过这个success判断
    private String message = "操作成功";
    private Integer code;  // 新增状态码字段
    //后续做文件上传这里还要加参数
    private Object resultObj;

    //操作成功调这个
    public AjaxResult(){}
    //方便增删改失败的时候调用有参 - 操作失败调这个
    public AjaxResult(Boolean success, String message) {
        this.success = success;
        this.message = message;
    }

    //成功和失败方法
    public static AjaxResult ok(){
        return new AjaxResult().setCode(StatusCode.SUCCESS.getCode());
    }
    public static AjaxResult ok(Object resultObj){
        return ok().setResultObj(resultObj);
    }
    public static AjaxResult fail(){
        return new AjaxResult()
                .setCode(StatusCode.OPERATION_FAILED.getCode())
                .setSuccess(false)
                .setMessage(StatusCode.OPERATION_FAILED.getMessage());
    }
    public static AjaxResult fail(Object resultObj){
        return fail().setResultObj(resultObj);
    }


    //链式语法改造
    public static AjaxResult me(){
        return new AjaxResult().setCode(StatusCode.SUCCESS.getCode());
    }
    public AjaxResult setSuccess(Boolean success) {
        this.success = success;
        return this;
    }
    public AjaxResult setMessage(String message) {
        this.message = message;
        return this;
    }
    public AjaxResult setCode(Integer code) {
        this.code = code;
        return this;
    }
    public AjaxResult setResultObj(Object resultObj) {
        this.resultObj = resultObj;
        return this;
    }
}
